//
//  MOHomeViewController.m
//  MobileOffice
//
//  Created by 晓 on 2019/5/30.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOHomeViewController.h"
#import "MOHomeViewCell.h"
#import "MOHomeModel.h"
#import "MOHomeReusableView.h"
#import "MOAddressBookViewController.h"
#import "MOOfficialListViewController.h"
#import "MONumberOfPersonnelViewController.h"
#import "MOGetMarriedViewController.h"
#import "MOMyShenPiViewController.h"
#import "MOMyOrderFoodViewController.h"

@interface MOHomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,SDCycleScrollViewDelegate>

@property(nonatomic,strong)UICollectionView *collectionView;

@property(nonatomic,strong)NSMutableArray *dataSource;
//顶部banner
@property(nonatomic,strong)SDCycleScrollView *cycleScrollView;
//newsView
@property(nonatomic,strong)UIView *newsView;
//newsTitle
@property(nonatomic,strong)UILabel *newsTitleLabel;

@end

@implementation MOHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"税务移动办公";//@"新疆维吾尔自治区税务局";
    
    [self.view addSubview:self.collectionView];
    
    [self initData];
    
    [self getNoNumber];
    
    [self getIpaInfoData];
}

-(void)initData
{
    self.dataSource = [NSMutableArray array];
    NSArray *nameArr = @[@"通讯录",@"综合办公",@"数字人事",@"我的审批",@"我的订餐",@""];
    NSArray *descArr = @[@"展示入口",@"综合办公一键直达",@"数字人事一键直达",@"流程审批一键直达",@"订餐通道入口",@""];
    NSArray *imageArr = @[@"shouguitongji",@"zonghebangong",@"renshi",@"jixiaoguanli",@"shijutongzhi",@""];
    for (int i = 0; i<nameArr.count; i++) {
        MOHomeModel *model = [[MOHomeModel alloc]init];
        model.nameStr = nameArr[i];
        model.descStr = descArr[i];
        model.imageStr = imageArr[i];
        [self.dataSource addObject:model];
    }
    [self.collectionView reloadData];
    
    self.cycleScrollView.imageURLStringsGroup = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];
}

-(void)getNoNumber
{
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",ODPSCX_LOGIN_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue]==0) {//成功
            
            UITabBarItem *item = self.tabBarController.tabBar.items[2];
            if ([responseObject[@"totalCount"] integerValue]>0) {
                if ([responseObject[@"totalCount"] integerValue]>99) {
                    item.badgeValue = @"99+";
                }else{
                    item.badgeValue = [NSString passNullForString:responseObject[@"totalCount"]];
                }
            }
        }
    } failure:^(NSError *error) {
    }];
}

-(void)getIpaInfoData
{
    //
    NSString *postUrl = [NSString stringWithFormat:@"%@?apkType=IOS&authorization=%@",APKINFO_GETAPKINFO_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"版本信息:%@",responseObject);
        
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        NSString *app_version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
        NSInteger current = [[app_version stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
        NSInteger new = [[[NSString passNullForString:responseObject[@"apk_version"]] stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
        if (new>current) {//有新版本
            MOHintView *hintView = [[MOHintView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) andContentStr:@"发现新版本,是否更新？"];
            hintView.clickSureButtonBlock = ^{
                if (@available(iOS 10.0, *)) {
                    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"https://www.pgyer.com/geKO"] options: @{} completionHandler: nil];
                } else {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.pgyer.com/geKO"]];
                }
            };
            [WINDOW addSubview:hintView];
        }
        
    } failure:^(NSError *error) {
        
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:WINDOW];
    }];
}

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
}

#pragma mark -UICollectionViewDataSource,UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MOHomeViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.item];
    return cell;
    
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(SCREEN_WIDTH/2, 43+35);
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
    
}

//定义每个UICollectionView 横向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

//定义每个UICollectionView 竖向的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(SCREEN_WIDTH,kHConversions(173)+10+24);

}

//头部显示的内容
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        MOHomeReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        
        [view addSubview:self.cycleScrollView];
        
//        [view addSubview:self.newsView];
        
        UIView *whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, self.cycleScrollView.bottom + 10, SCREEN_WIDTH, 24)];
        whiteView.backgroundColor = [UIColor whiteColor];
        [view addSubview:whiteView];
        
        view.backgroundColor = KGrayColor_f8f8f8;
        
        return view;
    }else{
        return nil;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {//通讯录
        MOAddressBookViewController *vc = [[MOAddressBookViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    if (indexPath.item == 1) {//综合办公
        MOOfficialListViewController *officialListVC = [[MOOfficialListViewController alloc] init];
        [self.navigationController pushViewController:officialListVC animated:YES];
        return;
    }
    
    if (indexPath.item == 2) {//结亲
        MONumberOfPersonnelViewController *numberOfPersonVC = [[MONumberOfPersonnelViewController alloc]init];
        [self.navigationController pushViewController:numberOfPersonVC animated:YES];
        return;
    }
    
    if (indexPath.item == 3) {//我的审批
        MOMyShenPiViewController *vc= [[MOMyShenPiViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    if(indexPath.item == 4){//我的订餐
        MOMyOrderFoodViewController *vc= [[MOMyOrderFoodViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    
}


#pragma mark -懒加载
-(UICollectionView *)collectionView
{
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-TabbarHeight-StatusBarAndNavigationBarHeight) collectionViewLayout:layout];
        _collectionView.backgroundColor = KGrayColor_f8f8f8;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[MOHomeViewCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[MOHomeReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
        
    }
    return _collectionView;
}


- (SDCycleScrollView *)cycleScrollView{
    if (!_cycleScrollView) {
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, kHConversions(173)) delegate:self placeholderImage:[UIImage imageNamed:@""]];
        _cycleScrollView.currentPageDotColor = KWhiteColor;
        _cycleScrollView.pageDotColor = [KWhiteColor colorWithAlphaComponent:0.5];
        _cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleClassic;
        _cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _cycleScrollView.backgroundColor = KWhiteColor;
        _cycleScrollView.autoScroll = YES;
        _cycleScrollView.delegate = self;
    }
    return _cycleScrollView;
}

-(UIView *)newsView
{
    if (!_newsView) {
        _newsView = [[UIView alloc]initWithFrame:CGRectMake(0, _cycleScrollView.bottom, SCREEN_WIDTH, kHConversions(44))];
        _newsView.backgroundColor = KWhiteColor;
        
        UIImageView *iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kWConversions(22), kHConversions(10), 118.0/55.0*kHConversions(28), kHConversions(28))];
        iconImageView.image = IMAGE(@"news_home");
        iconImageView.centerY = kHConversions(44)/2;
        [_newsView addSubview:iconImageView];
        
        self.newsTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(iconImageView.right + 10, 0, SCREEN_WIDTH - kWConversions(22)*2 - 10 - iconImageView.width, kHConversions(44))];
        self.newsTitleLabel.font = FONT_SIZE(12);
        self.newsTitleLabel.textColor = KBlackColor_2A2A2A;
        self.newsTitleLabel.text = @"关于2017年度企业所得税调整的通知";
        [_newsView addSubview:self.newsTitleLabel];
        
    }
    return _newsView;
}

@end
